package com.firmye.codereposboot.controller.product.json;

import com.firmye.codereposboot.controller.BaseController;
import com.firmye.codereposboot.model.commons.CustResponse;
import com.firmye.codereposboot.model.product.pojo.Product;
import com.firmye.codereposboot.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/proJ")
@Slf4j
public class ProductJsonController extends BaseController {

    @Autowired
    private ProductService productService;

    @PostMapping("/")
    public CustResponse cuProduct(Product product){
        String msg = "";
        try {
            if(null == product.getProId()){
                msg = "新增产品成功";
            }else{
                msg = "修改产品成功";
            }
            return success(msg,productService.cuProduct(product));
        }catch (Exception e){
            if(null == product.getProId()){
                msg = "新增产品失败";
            }else{
                msg = "修改产品失败";
            }
            return error(msg,e);
        }
    }

    @DeleteMapping("/{proIds}")
    public CustResponse delProduct(@PathVariable("proIds") String proIds){
        try {
            return success("删除产品成功",productService.delProducts(proIds));
        }catch (Exception e){
            return error("删除产品失败",e);
        }
    }

    @GetMapping("/pie")
    public CustResponse getPie(){
        try {
            return success("获取饼状图数据成功",productService.getPie());
        }catch (Exception e){
            return error("获取饼状图数据失败",e);
        }
    }

    @GetMapping("/")
    public CustResponse getProducts(){
        try {
            return success("获取产品列表成功",productService.getProducts());
        }catch (Exception e){
            return error("获取产品列表失败",e);
        }
    }

    @GetMapping("/bar")
    public CustResponse getBar(Integer year,Integer proId){
        try {
            return success("获取柱状图数据成功",productService.getBar(year,proId));
        }catch (Exception e){
            return error("获取柱状图数据失败",e);
        }
    }

    @GetMapping("/line")
    public CustResponse getLine(Integer year, Integer month, String proIds){
        try {
            return success("获取折线图数据成功",productService.getLine(year,month,proIds));
        }catch (Exception e){
            return error("获取折线图数据失败",e);
        }
    }

}
